package com.southminority.ethnic.controller.admin;

import com.southminority.ethnic.common.annotation.RequireAdmin;
import com.southminority.ethnic.common.context.UserContext;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.common.util.AdminAuthUtil;
import com.southminority.ethnic.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员控制器
 * 演示管理员权限验证功能
 * 
 * @author SouthMinority
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口", description = "管理员相关接口")
public class AdminController {

    /**
     * 获取管理员仪表板信息
     * 需要管理员权限（user_type >= 1）
     */
    @GetMapping("/dashboard")
    @Operation(summary = "获取管理员仪表板", description = "获取管理员仪表板信息，需要管理员权限")
    @RequireAdmin(1)
    public R<Map<String, Object>> getDashboard() {
        User currentUser = UserContext.getCurrentUser();
        
        Map<String, Object> dashboard = new HashMap<>();
        dashboard.put("welcome", "欢迎管理员：" + currentUser.getUsername());
        dashboard.put("userType", AdminAuthUtil.getUserTypeDescription(currentUser.getUserType()));
        dashboard.put("currentTime", System.currentTimeMillis());
        dashboard.put("systemStatus", "正常运行");
        
        return R.ok(dashboard);
    }

    /**
     * 获取系统统计信息
     * 需要普通管理员权限（user_type >= 1）
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取系统统计", description = "获取系统统计信息，需要管理员权限")
    @RequireAdmin(1)
    public R<Map<String, Object>> getStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        statistics.put("totalUsers", 1000);
        statistics.put("activeUsers", 850);
        statistics.put("totalDocuments", 500);
        statistics.put("totalLanguages", 50);
        statistics.put("systemUptime", "7天");
        
        return R.ok(statistics);
    }

    /**
     * 获取用户管理信息
     * 需要普通管理员权限（user_type >= 1）
     */
    @GetMapping("/users")
    @Operation(summary = "获取用户列表", description = "获取用户列表，需要管理员权限")
    @RequireAdmin(1)
    public R<Map<String, Object>> getUsers() {
        Map<String, Object> users = new HashMap<>();
        users.put("totalCount", 1000);
        users.put("adminCount", 50);
        users.put("normalUserCount", 950);
        users.put("message", "用户管理功能");
        
        return R.ok(users);
    }

    /**
     * 系统设置
     * 需要超级管理员权限（user_type >= 2）
     */
    @GetMapping("/settings")
    @Operation(summary = "系统设置", description = "系统设置功能，需要超级管理员权限")
    @RequireAdmin(2)
    public R<Map<String, Object>> getSystemSettings() {
        Map<String, Object> settings = new HashMap<>();
        settings.put("systemName", "南方少数民族语言文化资源平台");
        settings.put("version", "1.0.0");
        settings.put("maintenanceMode", false);
        settings.put("message", "系统设置功能，仅超级管理员可访问");
        
        return R.ok(settings);
    }

    /**
     * 测试权限验证
     * 用于测试不同用户类型的权限
     */
    @GetMapping("/test-auth")
    @Operation(summary = "测试权限验证", description = "测试权限验证功能")
    public R<Map<String, Object>> testAuth() {
        User currentUser = UserContext.getCurrentUser();
        Map<String, Object> result = new HashMap<>();
        
        result.put("currentUser", currentUser != null ? currentUser.getUsername() : "未登录");
        result.put("userType", currentUser != null ? currentUser.getUserType() : null);
        result.put("userTypeDescription", AdminAuthUtil.getUserTypeDescription(currentUser != null ? currentUser.getUserType() : null));
        result.put("isAdmin", AdminAuthUtil.isAdmin());
        result.put("isSuperAdmin", AdminAuthUtil.isSuperAdmin());
        result.put("isNormalAdmin", AdminAuthUtil.isNormalAdmin());
        result.put("isNormalUser", AdminAuthUtil.isNormalUser());
        result.put("hasPermission1", AdminAuthUtil.hasPermission(1));
        result.put("hasPermission2", AdminAuthUtil.hasPermission(2));
        
        return R.ok(result);
    }

    /**
     * 检查当前用户权限
     */
    @GetMapping("/check-permission")
    @Operation(summary = "检查权限", description = "检查当前用户的管理员权限")
    public R<Map<String, Object>> checkPermission() {
        User currentUser = UserContext.getCurrentUser();
        
        if (currentUser == null) {
            return R.fail("用户未登录");
        }
        
        Map<String, Object> permission = new HashMap<>();
        permission.put("userId", currentUser.getId());
        permission.put("username", currentUser.getUsername());
        permission.put("userType", currentUser.getUserType());
        permission.put("userTypeDescription", AdminAuthUtil.getUserTypeDescription(currentUser.getUserType()));
        permission.put("canAccessAdmin", AdminAuthUtil.isAdmin());
        
        return R.ok(permission);
    }
} 